package com.pkk.blny.web.controller.api;

import com.pkk.blny.model.domain.Tag;
import com.pkk.blny.model.dto.JsonResult;
import com.pkk.blny.model.enums.ResponseStatusEnum;
import com.pkk.blny.service.TagService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <pre>
 *     标签API
 * </pre>
 *
 * @author : RYAN0UP
 * @date : 2018/6/6
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/api/tags")
public class ApiTagController {

  @Autowired
  private TagService tagService;

  /**
   * 获取所有标签
   *
   * <p>
   * result json:
   * <pre>
   * {
   *     "code": 200,
   *     "msg": "OK",
   *     "result": [
   *         {
   *             "tagId": ,
   *             "tagName": "",
   *             "tagUrl": ""
   *         }
   *     ]
   * }
   *     </pre>
   * </p>
   *
   * @return JsonResult
   */
  @GetMapping
  public JsonResult tags() {
    List<Tag> tags = tagService.findAll();
    if (null != tags && tags.size() > 0) {
      return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), tags);
    } else {
      return new JsonResult(ResponseStatusEnum.EMPTY.getCode(), ResponseStatusEnum.EMPTY.getMsg());
    }
  }

  /**
   * 获取单个标签的信息
   *
   * <p>
   * result json:
   * <pre>
   * {
   *     "code": 200,
   *     "msg": "OK",
   *     "result": {
   *         "tagId": ,
   *         "tagName": "",
   *         "tagUrl": ""
   *     }
   * }
   *     </pre>
   * </p>
   *
   * @param tagUrl tagUrl
   * @return JsonResult
   */
  @GetMapping(value = "/{tagUrl}")
  public JsonResult tags(@PathVariable("tagUrl") String tagUrl) {
    Tag tag = tagService.findByTagUrl(tagUrl);
    if (null != tag) {
      return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), tag);
    } else {
      return new JsonResult(ResponseStatusEnum.NOTFOUND.getCode(), ResponseStatusEnum.NOTFOUND.getMsg());
    }
  }
}
